module disco.web {
    declare var OData;

    export class Requests {
        public static loadOntologyMetadata(url: string, success: (metadata) => void, error: (error) => void = null) {
            OData.read(
                url,
                success,
                error,
                OData.metadataHandler
                );
        }

        public static loadOntologyContent(url: string, success: (data) => void, error: (error) => void = null) {
            jQuery.ajax({
                headers: { Accept: "application/json;odata=fullmetadata" },
                url: url,
                success: success,
                error: (a, b, err) => { if (error) error(err) },
                cache: false
            });
        }

        public static addEntity(entitySetUrl: string, propertyValues: any, success: (data) => void, error: (error) => void = null) {
            jQuery.ajax({
                headers: { Accept: "application/json;odata=fullmetadata" },
                url: entitySetUrl,
                type: "POST",
                data: propertyValues,
                success: success,
                error: (a, b, err) => { if (error) error(err) },
                cache: false
            });
        }

        public static removeEntity(entityUrl: string, success: (data) => void, error: (error) => void = null) {
            jQuery.ajax({
                url: entityUrl,
                type: "DELETE",
                success: success,
                error: (a, b, err) => { if (error) error(err) },
                cache: false
            });
        }

        public static addEntityConnection(entityUrl: string, propertyUrl: string, valueUrl: string, success: (data) => void, error: (error) => void = null) {
            jQuery.ajax({
                url: entityUrl + '/$links/' + propertyUrl,
                type: "POST",
                data: JSON.stringify({ url: valueUrl }),
                processData: false,
                contentType: 'application/json; charset=UTF-8',
                success: success,
                error: (x, y, e) => { if (error) error(e) }
            });
        }

        public static removeEntityConnection(entityUrl: string, propertyUrlWithKey: string, success: (data) => void, error: (error) => void = null) {
            jQuery.ajax({
                url: entityUrl + '/$links/' + propertyUrlWithKey,
                type: "DELETE",
                success: success,
                error: (x, y, e) => { if (error) error(e) }
            });
        }
    }
}